/* The code examples copyrighted by Nokia Corporation that are included to
 * this material are licensed to you under following MIT-style License:
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "identitymodel.h"

IdentityModel::IdentityModel(QObject *parent) :
    QAbstractListModel(parent)
{
    QHash<int, QByteArray> roles;
    roles[IdentityIdRole] = "id";
    roles[CaptionRole] = "caption";
    setRoleNames(roles);

}

IdentityModel::~IdentityModel()
{
}

void IdentityModel::addIdentity(quint32 id, QString caption)
{
    QModelIndex parent;
    beginInsertRows(parent, m_identityList.size(), m_identityList.size());
    QModelIndex modelIndex=index(m_identityList.size(), 0);
    m_identityList.append(qMakePair(id, caption));
    endInsertRows();
    emit dataChanged(modelIndex, modelIndex);

}

void IdentityModel::removeIdentity(int row)
{
    QModelIndex parent;
    QModelIndex startIndex=index(row);
    QModelIndex endIndex=index(m_identityList.size());
    beginRemoveRows(parent, row, row);
    m_identityList.removeAt(row);
    endRemoveRows();
    emit dataChanged(startIndex, endIndex);
}

quint32 IdentityModel::identityID(int row)
{
    return m_identityList.at(row).first;
}

int IdentityModel::createdIdentitiesQty()
{
    return m_identityList.size();
}

QVariant IdentityModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    if (index.row() >= m_identityList.size())
        return QVariant();


        switch (role) {
            case IdentityIdRole:
                return m_identityList.at(index.row()).first;
            case CaptionRole:
                return m_identityList.at(index.row()).second;
        }

    return QVariant();
}

int IdentityModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return 0;

    return m_identityList.size();
}

